const express =require('express')
const Dictionary =require('../../Schema/dictionarySchema')
const Nation =require('../../Schema/nationSchema')
const Power =require('../../Schema/powerShema')
const router= express.Router()
// 学历字典的接口
router.get('/eduction',async(req,res)=>{
       const result =await Dictionary.find({})
       console.log(result)
       if (!result) {
        res.send({status:0,result:result})
        return
       }
       res.send({status:1,result:result})
})

router.post('/eduction',async(req,res)=>{
     const value=req.body.value
     const label =req.body.label
     const newDictionary=new Dictionary({
      value:value,
      label:label
     })
    const result =await newDictionary.save()
    console.log(result)
    if (!result) {
      res.send({status:0})
      return
    }
    res.send({status:1})
})
router.delete('/eduction/:value',async(req,res)=>{
  const value=req.params.value
  const result =await Dictionary.remove({value:value})
  console.log(result)
  if (result.n==1) {
    res.send({status:1})
    return
  }
  res.send({status:0})
})
// 民族字典的接口
router.get('/nation',async(req,res)=>{
  const result =await Nation.find({})
       console.log(result)
       if (!result) {
        res.send({status:0,result:result})
        return
       }
       res.send({status:1,result:result})
})

router.post('/nation',async(req,res)=>{
  const value=req.body.value
  const label =req.body.label
  const newNation =new Nation({
    value:value,
    label:label
  })
  const result =await newNation.save()
  console.log(result)
  if (!result) {
    res.send({status:0})
    return
  }
  res.send({status:1})
})
router.delete('/nation/:value',async(req,res)=>{
  const value=req.params.value
  const result =await Nation.remove({value:value})
  console.log(result)
  if (result.n==1) {
    res.send({status:1})
    return
  }
  res.send({status:0})
})
// 管理员权限字典接口
router.get('/power',async(req,res)=>{
  const result =await Power.find({})
  console.log(result)
  if (!result) {
   res.send({status:0,result:result})
   return
  }
  res.send({status:1,result:result})
})
router.post('/power',async(req,res)=>{
  const value=req.body.value
  const label =req.body.label
  const newPower =new Power({
    value:value,
    label:label
  })
  const result =await newPower.save()
  console.log(result)
  if (!result) {
    res.send({status:0})
    return
  }
  res.send({status:1})
})
router.delete('/power/:value',async(req,res)=>{
  const value=req.params.value
  const result =await Power.remove({value:value})
  console.log(result)
  if (result.n==1) {
    res.send({status:1})
    return
  }
  res.send({status:0})
})
module.exports=router